package com.gitee.wsl.text.regex.sample

import timber.log.Timber

// 正则表达式: 验证汉字
val Regex.Companion.CHINESE: Regex
    get() = "^[\u4e00-\u9fa5]+$".toRegex()

// 正则表达式: 验证汉字 ( 含双角符号 )
val Regex.Companion.CHINESE_ALL: Regex
    get() = "^[\u0391-\uFFE5]+$".toRegex()

// 正则表达式: 验证汉字 ( 含双角符号 )
val Regex.Companion.CHINESE_ALL2: Regex
    get() = "[\u0391-\uFFE5]".toRegex()

//ASCII码表中的全部的特殊字符
val Regex.Companion.ASCII: Regex
    get() = """[\x21-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]+""".toRegex()

/**
 * 校验汉字 ( 无符号, 纯汉字 )
 * @param str 待校验的字符串
 * @return `true` yes, `false` no
 */
fun String.isChinese(): Boolean {
    return Regex.CHINESE.matches(this)
}

/**
 * 判断字符串是不是全是中文
 * @param str 待校验的字符串
 * @return `true` yes, `false` no
 */
fun String.isChineseAll(): Boolean {
    return Regex.CHINESE_ALL.matches(this)
}

/**
 * 判断字符串中包含中文、包括中文字符标点等
 * @param str 待校验的字符串
 * @return `true` yes, `false` no
 */
fun String.isContainChinese(): Boolean {
    if (this.isNotEmpty()) {
        try {
            if (isNotEmpty()) {
                val dChar = toCharArray()
                for (i in indices) {
                    val flag = dChar[i].toString().matches(Regex.CHINESE_ALL2)
                    if (flag) {
                        return true
                    }
                }
            }
        } catch (e: Exception) {
            Timber.d(e, "isContainChinese")
        }
    }
    return false
}

/**
 * 中文字、英文字母、数字和下划线
 */
val Regex.Companion.GENERAL_WITH_CHINESE: Regex
    get() = "^[\u4E00-\u9FFF\\w]+$".toRegex()

/**
 * UUID
 */
val Regex.Companion.UUID: Regex
    get() = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$".toRegex()

/**
 * 不带横线的UUID
 */
val Regex.Companion.UUID_SIMPLE: Regex
    get() = "^[0-9a-fA-F]{32}$".toRegex()

